//
//  LTSendMessageModel.m
//  LTIM
//
//  Created by 俊松何 on 2017/3/31.
//  Copyright © 2017年 俊松何. All rights reserved.
//

#import "LTSendMessageModel.h"
#import "MJExtension.h"
#import "LTClient.h"
#import "LTSendMessgaeFinalModel.h"
#import "LTMessageCallBackModel.h"
@interface LTSendMessageModel()

@property(nonatomic,assign) BOOL sendSuccess;

@end
@implementation LTSendMessageModel
//封装发送的data
-(NSData*)requestSendData {
    
    LTSendMessgaeFinalModel *messageModel = [[LTSendMessgaeFinalModel alloc] initWithMessage:self.message];
    NSData  *messageData = [messageModel mj_JSONData];
    return messageData;
    
}
//解包 回包
-(int)onPostDecode:(NSData*)responseData
{
    
    NSString *messageJson = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    LTMessageCallBackModel *callbackModel = [LTMessageCallBackModel mj_objectWithKeyValues:messageJson];
    
    if(self.sendComplentBlock)
    {
        LTError *error = nil;
        if(callbackModel.code == LTMessageCallBackCodeTypeSuccess)
        {
            if( [callbackModel.data[@"createTime"] longValue] != 0)
            {
                self.message.createTime = [callbackModel.data[@"createTime"] longValue];
            }
            self.message.status = LTMessageStatusSuccessed;
        }else if(callbackModel.code == LTMessageCallBackCodeTypeNOPerssion)
        {
            self.message.status = LTMessageStatusNoPermission;
            if( [callbackModel.data[@"createTime"] longValue] != 0)
            {
                self.message.createTime = [callbackModel.data[@"createTime"] longValue];
            }
            
            error = [LTError errorWithDescription:@"没有权限,发送失败" code:callbackModel.code];
        }else
        {
            self.message.status = LTMessageStatusFailed;
            if( [callbackModel.data[@"createTime"] longValue] != 0)
            {
                self.message.createTime = [callbackModel.data[@"createTime"] longValue];
            }
            error = [LTError errorWithDescription:@"发送失败" code:callbackModel.code];
        }
        
         self.sendComplentBlock(self.message,error,callbackModel.code);
    }
    return 0;
}
//任务结束。   
-(int)onTaskEnd:(uint32_t)tid errType:(uint32_t)errtype errCode:(uint32_t)errcode
{
    if(self.sendComplentBlock)
    {
        if(errtype != 0){
            LTError *lterror = [LTError errorWithDescription:@"发送失败" code:-1];
            self.message.status = LTMessageStatusFailed;
            self.sendComplentBlock(self.message,lterror,LTMessageCallBackCodeTypeFailer);
        }
    }
    return 0;
}
@end
